﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;

namespace System.ApplicationModel.Composition
{
    /// <summary>
    /// Composition service
    /// </summary>
    public class CompositionContainerService : IApplicationService, IApplicationLifetimeAware, IDisposable
    {
        #region Constructors
        /// <summary>
        /// Finalizer for the <see cref="System.ApplicationModel.Composition.ViewExportAttribute"/> class.
        /// </summary>
        ~CompositionContainerService()
        {
            Dispose(false);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the service composition container
        /// </summary>
        public CompositionContainer Container { get; private set; }
        #endregion

        #region IApplicationService
        /// <summary>
        /// Called by an application in order to initialize the application extension service. 
        /// </summary>
        /// <param name="context">Provides information about the application state. </param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        public virtual void StartService(ApplicationServiceContext context)
        {
            this.Container = new CompositionContainer(new AggregateCatalog());
#if SILVERLIGHT
            CompositionHost.Initialize(this.Container);
#endif
        }

        /// <summary>
        /// Called by an application in order to stop the application extension service. 
        /// </summary>
        public virtual void StopService()
        {

        }
        #endregion

        #region IApplicationLifetimeAware
        /// <summary>
        /// Called by an application immediately after the <see cref="System.Windows.Application.Exit"/> event occurs. 
        /// </summary>
        public virtual void Exited()
        {

        }

        /// <summary>
        /// Called by an application immediately before the <see cref="System.Windows.Application.Exit"/> event occurs. 
        /// </summary>
        public virtual void Exiting()
        {

        }

        /// <summary>
        /// Called by an application immediately after the <see cref="System.Windows.Application.Startup"/> event occurs. 
        /// </summary>
        public virtual void Started()
        {

        }

        /// <summary>
        /// Called by an application immediately before the <see cref="System.Windows.Application.Startup"/> event occurs. 
        /// </summary>
        public virtual void Starting()
        {

        }
        #endregion

        #region IDisposable Implementation
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <param name="disposing">True if managed ressources must be released ; otherwise false.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.Container != null)
                {
                    this.Container.Dispose();
                    this.Container = null;
                }
            }
        }
        #endregion
    }
}
